/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.uc.manager.impl;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.xy.biz.uc.domain.po.SignaturePO;
import com.xy.tool.context.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.biz.uc.domain.bo.SignatureBO;
import com.xy.biz.uc.domain.query.SignatureQuery;
import com.xy.biz.uc.domain.converter.SignatureConverter;
import com.xy.biz.uc.manager.SignatureManager;
import com.xy.biz.uc.dao.SignatureDao;
import org.springframework.util.CollectionUtils;
import sun.misc.BASE64Decoder;

/**
 *
 *
 * @author yzh
 * @date 2021-2-22
 */
@Service
public class SignatureManagerImpl implements SignatureManager {

    @Autowired
    private SignatureDao signatureDao;

    @Override
    public int saveSignature(SignatureBO signatureBO) {
        try {
            SignaturePO signaturePO = SignatureConverter.toPO(signatureBO);
            signaturePO.setSign(signatureBO.getSign().getBytes(StandardCharsets.UTF_8));
            return signatureDao.saveSignature(signaturePO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public int saveAllSignature(Collection<SignatureBO> collection) {
        return signatureDao.saveAllSignature(SignatureConverter.toPO(collection));
    }

    @Override
    public int updateSignature(SignatureBO signatureBO) {
        try {
            SignaturePO signaturePO = SignatureConverter.toPO(signatureBO);
            signaturePO.setSign(signatureBO.getSign().getBytes(StandardCharsets.UTF_8));
            if (signaturePO.getUserName() == null) {
                signaturePO.setUserName(UserContextHolder.getUserName());
            }
            return signatureDao.updateSignature(signaturePO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public int deleteSignature(Long signatureId) {
        return signatureDao.deleteSignature(signatureId);
    }

    @Override
    public SignatureBO getSignature(Long signatureId) {
        return SignatureConverter.fromPO(signatureDao.getSignature(signatureId));
    }

    @Override
    public List<SignatureBO> listSignatureById(Collection<Long> collection) {
        return SignatureConverter.fromPO(signatureDao.listSignatureById(collection));
    }

    /**
     * 查找多个
     *
     * @param collection 标识集合
     */
    @Override
    public List<SignatureBO> listSignatureByUserId(Collection<Long> collection) {
        List<SignaturePO> signaturePOS = signatureDao.listSignatureByUserId(collection);
        Map<Long, byte[]> map = signaturePOS.stream().collect(Collectors.toMap(SignaturePO::getSignatureId, SignaturePO::getSign));
        List<SignatureBO> signatureBOS = SignatureConverter.fromPO(signaturePOS);
        signatureBOS = signatureBOS.stream().peek(s -> {
            s.setSign(new String(map.get(s.getSignatureId()), StandardCharsets.UTF_8));
            s.setSign1(map.get(s.getSignatureId()));
        }).collect(Collectors.toList());
        return signatureBOS;
    }

    @Override
    public List<SignatureBO> listSignature(SignatureQuery query) {
        return SignatureConverter.fromPO(signatureDao.listSignature(query));
    }

    @Override
    public int countSignature(SignatureQuery query) {
        return signatureDao.countSignature(query);
    }

    @Override
    public SignatureBO getSignatureByUserId() {
        try {
            SignatureQuery query = SignatureQuery.one().userId(UserContextHolder.getUserId()).build();
            List<SignaturePO> signaturePOS = signatureDao.listSignature(query);
            if (!CollectionUtils.isEmpty(signaturePOS)) {
                SignaturePO signaturePO = signaturePOS.get(0);
                SignatureBO signatureBO = SignatureConverter.toBO(signaturePO);
                signatureBO.setSign(new String(signaturePO.getSign(), "UTF-8"));
                return signatureBO;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return null;
    }


}
